#include <iostream>
#include "shunxu.h"

#define eleType int



void init(SequentialList *list,int capacity){
    list->elements = new eleType[capacity];
    list->size = 0;
    list->capacity = capacity;
}

int getsize(SequentialList *list){
    return list->size;
}

void deleteElements(SequentialList *list){
    delete [] list->elements;
}

bool isEmpty(SequentialList *list){
    return list->size == 0;
}

void insert(SequentialList *list, int index, eleType value){
    if (index<0 || index>list->size){
        throw std::invalid_argument("invalid index");
    }
    if (list->size == list->capacity){
        int newcapacity = list->capacity*2;
        eleType *newElement = new eleType[newcapacity];
        for (int i = 0; i < list->size; ++i){
            newElement[i] = list->elements[i];
        }
        delete [] list->elements;
        list->elements = newElement;
        list->capacity = newcapacity;
    }
    for (int i = list->size; i > index; i--){
        list->elements[i] = list->elements[i-1];
    }
    list->elements[index] = value;
    list->size ++;
}

void removeElement(SequentialList *list, int index){
    if (index<0 || index>=list->size){
        throw std::invalid_argument("invalid index");
    }
    for (int i = index; i<list->size-1; i++){
        list->elements[i] = list->elements[i+1];
    }
    list->size --;
}

int findEleindex(SequentialList *list, eleType value){
    for (int i = 0; i < list->size; i++){
        if (list->elements[i] == value){
            return i;
        }
    }
    return -1;
}

eleType getElement(SequentialList *list, int index){
    if (index<0 || index>=list->size){
        throw std::invalid_argument("invalid index");
    }
    return list->elements[index];
}

void changeElement(SequentialList *list, int index, eleType value){
    if (index<0 || index>=list->size){
        throw std::invalid_argument("invalid index");
    }
    list->elements[index] = value;
}